﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Coding4FunCamera
{
    public partial class DirectoryBrowser : Form
    {
        string substringDirectory;

        private static string _personaldirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        public DirectoryBrowser()
        {
            InitializeComponent();
            string path =  Environment.GetFolderPath(Environment.SpecialFolder.Programs);
            
            string rootdirectory = Directory.GetDirectoryRoot(path);
            TreeNode rootNode = DirectoryTreeView.Nodes.Add(rootdirectory);
            DirectoryTreeView.ImageList = directoryImages;
            PopulateTree(rootdirectory, rootNode);
            rootNode.Expand();
        
        }
        public static string PersonalDirectory
        {
            get
            {
                return _personaldirectory;
            }
        }
        public string SelectedDirectory
        {
            get
            {
                return lblCurrentSelection.Text;
            }

        }
        public void PopulateTree(string directoryValue, TreeNode parentNode)
        {
            string[] directoryArray =
            Directory.GetDirectories(directoryValue);

            try
            {
                if (directoryArray.Length != 0)
                {
                    foreach (string directory in directoryArray)
                    {
                        substringDirectory = directory.Substring(
                        directory.LastIndexOf('\\') + 1,
                        directory.Length - directory.LastIndexOf('\\') - 1);

                        TreeNode myNode = new TreeNode(substringDirectory);
                        myNode.ImageIndex = 0;
                        myNode.SelectedImageIndex = 1;
                        
                        parentNode.Nodes.Add(myNode);

                        PopulateTree(directory, myNode);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                parentNode.Nodes.Add("Access denied");
            } // end catch

        }

        private void ExpandPersonalDirectory(DirectoryInfo d, ref TreeNode t)
        {
            if (d.FullName.Equals(_personaldirectory))
            {
                t.Expand();
            }
        }

        private void DirectoryView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            lblCurrentSelection.Text = e.Node.FullPath.Replace(@"\\", @"\");
        }

        private void menuOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void menuCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}